import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:steam_app/config/app_routes.dart';
import 'package:steam_app/config/app_theme.dart';
import '../../provider/user_provider.dart';
import '../library/library_page.dart';

class MenuInfoPage extends StatelessWidget {
  final Function(int) onNavigate; //接收用于导航的回调函数

  const MenuInfoPage({super.key, required this.onNavigate});

  @override
  Widget build(BuildContext context) {
    final user = context.watch<UserProvider>().currentUser;

    if (user == null) {
      return const Center(child: CircularProgressIndicator());
    }

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('选项'),
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.only(bottom: 24.0),
        children: [
          //商店（可伸缩）
          ExpansionTile(
            leading: const Icon(Icons.local_offer),
            title: Row(
              children: [
                const Text('商店'),
                const SizedBox(width: 8),
                //显示余额
                Text(
                  '(¥${user.balance.toStringAsFixed(2)})',
                  style: TextStyle(color: balanceTextColor, fontSize: 14),
                ),
              ],
            ),
            children: <Widget>[
              _buildSubMenuItem('商店主页'),
              _buildSubMenuItem('优惠'),
              _buildSubMenuItem('新品'),
              _buildSubMenuItem('探索队列'),
              _buildSubMenuItem('点数商店'),
              _buildSubMenuItem('愿望单'),
              _buildSubMenuItem('钱包'),
            ],
          ),

          //其他菜单项
          _buildMenuItem(icon: Icons.article, title: '新闻', onTap: () => onNavigate(1)), // 点击切换到资讯页
          _buildMenuItem(icon: Icons.shield, title: 'Steam 令牌', onTap: () => onNavigate(2)), // 点击切换到安全页
          _buildMenuItem(icon: Icons.notifications, title: '通知', onTap: () => onNavigate(3)),
          _buildMenuItem(icon: Icons.check, title: '确认'),

          //库
          _buildMenuItem(icon: Icons.grid_view_sharp, title: '库', onTap: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) => LibraryPage()));
          }),

          //社区
          ExpansionTile(
            leading: const Icon(Icons.people),
            title: const Text('社区'),
            children: <Widget>[
              _buildSubMenuItem('社区主页'),
              _buildSubMenuItem('讨论区'),
              _buildSubMenuItem('市场'),
              _buildSubMenuItem('直播'),
              _buildSubMenuItem('创意工坊'),
            ],
          ),

          //聊天
          _buildMenuItem(icon: Icons.sms, title: '聊天'),

          const Divider(height: 32),

          //底部链接和版权信息
          _buildFooterLink('对此应用进行反馈'),
          _buildFooterLink('设置'),
          _buildFooterLink('客服'),
          _buildFooterLink('获取 Steam 流式应用'),
          
          Padding(
            padding: EdgeInsets.symmetric(horizontal: hPaddingSize),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Image.asset(valve_logo, height: 20),
                Text(
                  '© ${DateTime.now().year} Valve Corporation. 保留所有权利。',
                  style: TextStyle(color: dimTextColor, fontSize: 12),
                  textAlign: TextAlign.left,
                ),
              ],
            ),
          )

        ],
      ),
    );
  }

  //创建标准的菜单项
  Widget _buildMenuItem({required IconData icon, required String title, VoidCallback? onTap}) {
    return ListTile(
      leading: Icon(icon),
      title: Text(title),
      onTap: onTap,
    );
  }

  //创建伸缩菜单的子项
  Widget _buildSubMenuItem(String title, {VoidCallback? onTap}) {
    return ListTile(
      contentPadding: const EdgeInsets.only(left: 72.0),
      title: Text(title),
      onTap: onTap,
    );
  }

  //创建底部的链接
  Widget _buildFooterLink(String title, {VoidCallback? onTap}) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 4.0),
        child: TextButton(
          onPressed: onTap ?? () {},
          child: Text(title, style: TextStyle(color: Colors.grey[400])),
        ),
      ),
    );
  }
}